﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FHJ.FHPI.PushNotifications.Library.GoogleC2DM;
using MySql.Data.MySqlClient;
using FHJ.FHPI.PushNotifications.Library;
using System.Collections.Specialized;
using System.Net;
using System.Threading.Tasks;

namespace FHJ.FHPI.PushNotifications.TestApp
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnStartService_Click(object sender, EventArgs e)
        {
            try
            {
                // Prevent the WebClient from sending these headers --> Google responses with Http error code 417 if they are set...
                System.Net.ServicePointManager.Expect100Continue = false;

                DbWrapper.Initialize(Properties.Settings.Default.MySql_ConnectionString);
                FHJ.FHPI.PushNotifications.Library.GoogleC2DM.AuthProvider.Instance.Initialize(
                    Properties.Settings.Default.GoogleC2DM_Username,
                    Properties.Settings.Default.GoogleC2DM_Password,
                    Properties.Settings.Default.GoogleC2DM_AuthRefreshIntervalSeconds);
                FHJ.FHPI.PushNotifications.Library.GoogleC2DM.MessageProcessor.Start();

                MessageBox.Show(this, "Successfully started service.", "Start service", MessageBoxButtons.OK, MessageBoxIcon.Information);               
            }
            catch (Exception exc)
            {
                MessageBox.Show(this, String.Format("Error starting service: {0}", exc.ToString()), "Start service", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnStopService_Click(object sender, EventArgs e)
        {
            try
            {
                FHJ.FHPI.PushNotifications.Library.GoogleC2DM.MessageProcessor.Stop();

                MessageBox.Show(this, "Successfully stopped service.", "Start service", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception exc)
            {
                MessageBox.Show(this, String.Format("Error starting service: {0}", exc.ToString()), "Start service", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
